package com.xqboss.web.controller.system;

import com.xqboss.apps.domain.sys.SysAnnouncementMsg;
import com.xqboss.apps.dto.system.PopupSaleableDto;
import com.xqboss.apps.dto.system.SysAnnouncementMsgDto;
import com.xqboss.apps.service.sys.SysAnnouncementMsgService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 公告消息表接口
 *
 * @author wangcong
 * @date 2023-08-31
 */
@Api(tags = "公告管理")
@RestController
@RequestMapping("/web/announcement/msg")
public class WebSysAnnouncementMsgController extends BaseController {

    @Autowired
    private SysAnnouncementMsgService sysAnnouncementMsgService;

    @PreAuthorize("@ss.hasPermi('web:announcement:add')")
    @ApiOperation("新增公告消息")
    @PostMapping("add")
    @Log(title = "公告", businessType = BusinessType.INSERT)
    public R add(@RequestBody SysAnnouncementMsg dto) {
        sysAnnouncementMsgService.addAnnouncementMsg(dto);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:announcement:info')")
    @ApiOperation("公告消息详情")
    @GetMapping("info/{id}")
    public R<SysAnnouncementMsg> findByKey(@PathVariable("id")
                                           @ApiParam(value = "公告消息ID", required = true)
                                           @NotNull(message = "ID不能为空")
                                           Long id) {
        SysAnnouncementMsg msg = sysAnnouncementMsgService.getAnnouncementDetail(id);
        return R.ok(msg);
    }

    @PreAuthorize("@ss.hasPermi('web:announcement:update')")
    @ApiOperation("编辑公告消息")
    @PutMapping("update")
    @Log(title = "公告", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SysAnnouncementMsg dto) {
        sysAnnouncementMsgService.updateAnnouncementMsg(dto);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:announcement:list')")
    @ApiOperation("公告消息列表")
    @PostMapping("list")
    public R<List<SysAnnouncementMsg>> findBy(@RequestBody SysAnnouncementMsgDto dto) {
        dto.startPageAndOrder();
        List<SysAnnouncementMsg> list = sysAnnouncementMsgService.findBy(dto);
        return R.ok(list);
    }


    @PreAuthorize("@ss.hasPermi('web:announcement:update-saleable')")
    @ApiOperation("上下架")
    @PutMapping("update-saleable")
    @Log(title = "公告", businessType = BusinessType.UPDATE)
    public R updateSaleable(@Validated @RequestBody PopupSaleableDto popup) {
        sysAnnouncementMsgService.changeSaleable(popup.getId(), popup.getSaleable());
        return R.ok();
    }
}
